﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using libDAL;
using System.Data;
using System.Windows.Forms;

namespace libCommonTools.BUS
{
    public class clsCommonFunctions : IDisposable 
    {
        private struct Queries
        {
            public string get_CompanyName(string strID)
            {
                return "select comname, comid " +
                       "from mfcompany " +
                       "where ltrim(rtrim(comid)) = '" + strID + "'";
            }

            public string get_AreaName(string strAreaID)
            {
                return "SELECT areaname, areacode " +
                       "FROM mfarea " +
                       "WHERE LTRIM(RTRIM(areacode)) = '" + strAreaID + "'";
            }

            public string get_StateName(string strStateID)
            {
                return "SELECT statename, statecode  " +
                       "FROM mfstate " +
                       "where ltrim(rtrim(statecode)) = '" + strStateID + "' ";
            }

            public string get_CityName(string strCityID)
            {
                return "SELECT cityname, citycode " +
                       "FROM mfcity " +
                       "WHERE LTRIM(RTRIM(citycode)) = '" + strCityID + "'";
            }

            public string get_CountryName(string strCountryID)
            {
                return "SELECT coudes, coucode  " +
                       "FROM mfcountry " +
                       "where ltrim(rtrim(coucode)) = '" + strCountryID + "'";
            }

            public string get_PortName(string strPortID)
            {
                return "SELECT PortName, PortID " +
                       "FROM mfport " +
                       "WHERE LTRIM(RTRIM(PortID)) = '" + strPortID + "'";
            }

            public string get_UserName(string strUserID)
            {
                return "select name,staid " +
                       "from mfstaff " +
                       "where ltrim(rtrim(staid)) = '" + strUserID + "'";
            }

            public string get_MaxID
            {
                get
                {
                    return "select substring(comid,2,LEN(comid) -1),* from mfcompany ";
                }
            }
        }

        private string get_QueryType(string strcountrycitycompanystateareaportuserID, string strcountrycitycompanystateareaportuser)
        {
            string strType = string.Empty;
            if (strcountrycitycompanystateareaportuser.ToLower() == "country")
            {
                strType = QueriesL.get_CountryName(strcountrycitycompanystateareaportuserID); 
            }
            else if (strcountrycitycompanystateareaportuser.ToLower() == "city")
            {
                strType = QueriesL.get_CityName(strcountrycitycompanystateareaportuserID); 
            }
            else if (strcountrycitycompanystateareaportuser.ToLower() == "company")
            {
                strType = QueriesL.get_CompanyName(strcountrycitycompanystateareaportuserID); 
            }
            else if (strcountrycitycompanystateareaportuser.ToLower() == "state")
            {
                strType = QueriesL.get_StateName(strcountrycitycompanystateareaportuserID);
            }
            else if (strcountrycitycompanystateareaportuser.ToLower() == "area")
            {
                strType = QueriesL.get_AreaName(strcountrycitycompanystateareaportuserID); 
            }
            else if (strcountrycitycompanystateareaportuser.ToLower() == "port")
            {
                strType = QueriesL.get_PortName(strcountrycitycompanystateareaportuserID); 
            }
            else if (strcountrycitycompanystateareaportuser.ToLower() == "user")
            {
                strType = QueriesL.get_UserName(strcountrycitycompanystateareaportuserID); 
            }
            return strType;
        }

        public string get_CodeName(string strcountrycitycompanystateareaport,string strID, cls_DAL _DAL)
        {
            string strName = string.Empty;
            try
            {
                strName = _DAL.get_OneKey(get_QueryType(strID, strcountrycitycompanystateareaport), "String"); 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " get_CodeName");
            }

            return strName;
        }

        private Queries QueriesL;

        public string get_HistoryID(string strCompanyID, cls_DAL _DAL)
        {
            return strCompanyID + "_" + clsGenFunction.get_ServerDateTimeRAW(_DAL); 
        }



        public string get_newCompanyID(string strCompanyName,cls_DAL _DAL)
        {

            string strNew = string.Empty;
            try
            {
                if (strCompanyName.Trim() != string.Empty)
                {
                    strNew = strCompanyName.Substring(0, 1);
                    Queries QueriesL = new Queries();
                    strNew += (Convert.ToInt32( _DAL.get_OneKey(QueriesL.get_MaxID, "String")) + 1).ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " get_newCompanyID"); 
            }
            return strNew;
        }

        public void Dispose()
        {
            GC.Collect();
            //throw new NotImplementedException();
        }
    }
}
